﻿using Meirimoney.Exchange.Website.Utilities;
using SuperMoney.Datas.DbModels;
using SuperMoney.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Meirimoney.Exchange.Website.Exchange
{
    public partial class List : LoginBase
    {
        /// <summary>
        /// 订单状态
        /// </summary>
        private OrderStatus status;
   
        /// <summary>
        ///订单的状态 
        /// </summary>
        public OrderStatus Status
        {
            get { return status; }
        }

        /// <summary>
        /// 当前页码
        /// </summary>
        private int pageIndex;

        /// <summary>
        /// 当前页码
        /// </summary>
        public int PageIndex
        {
            get { return pageIndex; }
        }

        /// <summary>
        /// 订单列表
        /// </summary>
        private List<ConsumeOrder> orders;


        /// <summary>
        /// 订单列表
        /// </summary>
        public List<ConsumeOrder> Orders
        {
            get { return orders; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.IsHttpGet)
            {
                this.OrdersBind();
            }
        }

        /// <summary>
        /// 数据绑定
        /// </summary>
        private void OrdersBind()
        {
            this.status = this.ParseStatus("s");
            this.pageIndex = this.ParsePageIndex("i");
            if (this.status == OrderStatus.Untreated)
            {
                this.orders = ConsumeOrderService.Instance.GetUntreatedOrders(this.pageIndex);
            }
        }

        /// <summary>
        /// 解析当前页码信息
        /// </summary>
        /// <returns></returns>
        private int ParsePageIndex(string key)
        {
            int index = 1;
            if (!string.IsNullOrEmpty(this.QueryValue(key)))
            {
                if (int.TryParse(this.QueryValue(key), out index))
                {
                    return index;
                }
            }
            return index;
        }

        /// <summary>
        /// 解析订单状态信息
        /// </summary>
        /// <param name="key">查询字符串中的key</param>
        /// <returns></returns>
        private OrderStatus ParseStatus(string key)
        {
            OrderStatus status = OrderStatus.Untreated;
            if (!string.IsNullOrEmpty(this.QueryValue(key)))
            {
                byte value;
                byte.TryParse(this.QueryValue(key), out value);
                if (Enum.IsDefined(typeof(OrderStatus), value))
                {
                    status = (OrderStatus)value;
                }
            }
            return status;
        }


        /// <summary>
        /// 返回指定状态的页数
        /// </summary>
        private int Count(int status)
        {
            return ConsumeOrderService.Instance.GetPageCount(status);
        }


        #region UI处理的方法

        /// <summary>
        /// 当前订单状态的页数
        /// </summary>
        protected int PageCount
        {
            get
            {
                return this.Count((byte)this.status);
            }
        }

        protected string CurrentStatus
        {
            get
            {
                return this.status.ToString("D");
            }
        }

        /// <summary>
        /// 根据发货地址识别操作类别
        /// </summary>
        /// <param name="shipAddress">发货地址</param>
        /// <returns>操作类型</returns>
        protected string OperationalTaxonomic(string shipAddress)
        {
            shipAddress = this.Server.UrlDecode(shipAddress);
            if (new Regex(@"^1[3|4|5|8][0-9]\d{8}$").IsMatch(shipAddress))
            {
                return "话费充值";
            }
            else
            {
                return "支付宝提现";
            }
        }
        #endregion
    }
}